﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;
using System;

namespace GBT.Tool.Core
{
    public class WXServiceAPI
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        static HttpRequest hr = new HttpRequest();
        static string accessToken = "";
        static int expires_in = 0;
        static DateTime dtTime = new DateTime();

        public string getOpenID(string jscode)
        {
            try
            {
                string appid = "wx96c5442a76d1b555";
                string secret = "427c1a765e5b0cf1e98fddfad8a94359";

                //string AccessTokenStr = hr.GBTGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid +
                //    "&secret=" + secret);

                //JObject joAccessToken = JObject.Parse(AccessTokenStr);
                //string accessToken = (string)joAccessToken["access_token"];

                string JSCodeStr = hr.GBTGet("https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret +
                    "&js_code=" + jscode + "&grant_type=authorization_code");

                JObject joJSCode = JObject.Parse(JSCodeStr);
                string openid = (string)joJSCode["openid"];
                string session_key = (string)joJSCode["session_key"];

                return openid;
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                return "";
            }
        }

        private static void getAccessTokenStr()
        {
            try
            {
                string appid = "wx96c5442a76d1b555";
                string secret = "427c1a765e5b0cf1e98fddfad8a94359";

                string AccessTokenStr = hr.GBTGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid +
                    "&secret=" + secret);

                JObject joAccessToken = JObject.Parse(AccessTokenStr);
                accessToken = (string)joAccessToken["access_token"];
                expires_in = (int)joAccessToken["expires_in"];
                dtTime = System.DateTime.Now;
                logger.Info(joAccessToken);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
        }

        public static string AccessToken
        {
            get
            {
                DateTime dtNow = DateTime.Now;
                DateTime dtOld = dtTime;

                TimeSpan ts = new TimeSpan();
                ts = dtNow - dtOld;

                if (ts.TotalSeconds > expires_in)
                {
                    getAccessTokenStr();
                }
                logger.Info(ts.TotalSeconds);
                return accessToken;
            }
        }

        public void sendMealMessage(JObject jo)
        {
            try
            {
                string jsonParam = JsonConvert.SerializeObject(jo);
                hr.GBTPost("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + AccessToken, jsonParam);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
        }
    }
}
